// pages/user-my-order-list/user-my-order-list.js
var app = getApp();
Page({
 
  /** 
   * 页面的初始数据
   */
  data: {
    type: "", //
    pagenum: 1, // 页码 
    pagesize: 10, //行数 
    more: 0, //分页
    dingdan: "", //订单数据
    imgUrl: app.globalData.imgUrl, //拼接图片路径
    localImg: app.globalData.staticimgUrl,  // 本地图片
    id: "", //取消订单ID
    button:true,//完成按钮隐藏   
  }, 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      type: options.type
    });
  },
  onShow: function(){
    this.quorder();
   
  },
  //区分订单
  quorder: function () {
    var url = '';
    let { type } = this.data;
    if (type == 1) {
      url = 'shopp/orderApi/getPendingPayment'; //待付款
    } else if (type == 2) {
      url = 'shopp/orderApi/getWaitCompleted'; //待完成
    } else if (type == 3) {
      url = 'shopp/orderApi/getWaitEvaluate'; //待评价
    } else if (type == 4) {
      url = 'shopp/orderApi/getAfterSaleOrder'; //退换货
    } else {
      url = 'shopp/orderApi/getAllOrder'; //全部
    }
    this.order(url); // 获取用户所有订单
  },
  // 获取用户订单状态
  order: function (url) {
    var that = this;
    let { pagenum, pagesize, more, dingdan} = this.data;
    app.request({
      url: url, //仅为示例，并非真实的接口地址
      data: {
        pagenum: pagenum, // 页码
        pagesize: pagesize, //行数
      },
      success(res) {
        if (res.data) {
          if (more == 0) {
            that.setData({
              'dingdan': res.data,
            })
          } else {
            wx.hideLoading();
            that.setData({
              more: 0,
            })
            if (res.data.length) {
              var info = dingdan.concat(res.data);
              that.setData({
                'dingdan': info,
              })
            } else {
              wx.showToast({
                title: '已无更多...',
                icon: 'none',
                duration: 1000,
              });
            }
          }
        }
      }
    })
  },
  //订单跳转 1是术前术后  2是医美
  jop: function (e) {
    var that = this;
    var orderType = e.currentTarget.dataset.ordertype;
    var orderId = e.currentTarget.dataset.orderid;
    this.setData({
      pagenum: 1
    });
    if (orderType == 1) {
      //术前术后
      wx.navigateTo({
        url: '../user-my-order-details/user-my-order-details?id=' + orderId + '&orderType=' + orderType,
      })
    } else {
      //医美
      wx.navigateTo({
        url: '../user-my-order-details-medical-1/user-my-order-details-medical-1?id=' + orderId + '&orderType=' + orderType,
      })
    }
  },
  //待安排查看日程
  see: function() {
    wx.showModal({
      title: '提示',
      content: '暂无安排，请耐心等待，我们会尽快联系您给您安排',
      success: function(res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    });
  },
  //没发货查看物流提示
  log:function(){
    wx.showModal({
      title: '提示',
      content: '正在安排，请耐心等待，我们会尽快给您发货',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //确认收货
  receipt: function(e) {
    var that = this;
    app.request({
      url: 'shopp/goodsOrderApi/confirmCollectGoods',
      data: {
        id: e.currentTarget.dataset.orderid
      },
      success(res) {
        wx.switchTab({
          url: '/pages/doctor-my/doctor-my',
        })
        wx.showToast({
          title: '收货成功',
          icon: 'success',
          duration: 1000,
        })
      }
    })
  },
  //取消订单按钮 1是术前术后  2是医美
  qxorder: function(e) {
    var that = this;
    let sid = e.currentTarget.dataset.sid;
    let ordertype = e.currentTarget.dataset.ordertype;
    if (ordertype == 2) {
      //删除医美订单
      app.request({
        url: 'shopp/yimeiGoodsOrderApi/cancelOrder',
        data: {
          id: sid,
        },
        success(res) {
          wx.showToast({
            title: "成功",
            icon: 'success',
            duration: 1000,
            success: function(){
              that.quorder();
            }
          });
        }
      })
    } else if (ordertype == 1) {
      //删除术前术后订单
      app.request({
        url: 'shopp/goodsOrderApi/cancelOrder',
        data: {
          id: sid,
        },
        success(res) {
          wx.showToast({
            title: "成功",
            icon: 'success',
            duration: 1000,
            success: function () {
              that.quorder();
            }
          });
        }
      })
    }
  },
  //评价页
  ping: function (e) {
    var that = this;
    var orderType = e.currentTarget.dataset.ordertype;
    var orderId = e.currentTarget.dataset.orderid;  
    wx.navigateTo({
      url: '../user-my-order-list-ping/user-my-order-list-ping?id=' + orderId + '&orderType=' + orderType,
    })
     
  },
  //查看订单 1是术前术后  2是医美
  look: function(e) {
    var that = this;
    var orderType = e.currentTarget.dataset.ordertype;
    var orderId = e.currentTarget.dataset.orderid;
    if (orderType == 1) {
      wx.navigateTo({
        url: '../user-my-order-details/user-my-order-details?id=' + orderId + '&orderType=' + orderType,
      })
    } else {
      wx.navigateTo({
        url: '../user-my-order-details-medical-1/user-my-order-details-medical-1?id=' + orderId + '&orderType=' + orderType,

      })
    }
  },
  //发起支付请求
  fkorder: function(e) {
    var item = e.currentTarget.dataset.item;
    if (item.orderType == 1){
      //术前术后待支付
      if (item.ifPayment){
        wx.navigateTo({
          url: `../user-shop-goods-order-pay/user-shop-goods-order-pay?res=${item.orderId}&totalMoney=${item.paymentMoney}&payType=1&orderType=1`,
        });
      } else {
        wx.navigateTo({
          url: `../user-shop-goods-balance-pay/user-shop-goods-balance-pay?res=${item.orderId}&balance=${item.useBalance}`,
        });
      }
    }
    else{
      //医美
      if (item.orderStateCode == 2){
        //支付尾款
        wx.navigateTo({
          url: `../user-service-order-pay/user-service-order-pay?orderId=${item.orderId}&storePayment=${item.storePayment}`,
        })
      }
      else if (item.orderStateCode == 0) {
        //支付预付款
        if (item.ifPayment) {
          wx.navigateTo({
            url: `../user-shop-goods-order-pay/user-shop-goods-order-pay?res=${item.orderId}&totalMoney=${item.paymentMoney}&payType=1&orderType=1`,
          });
        } else {
          wx.navigateTo({
            url: `../user-shop-goods-balance-pay/user-shop-goods-balance-pay?res=${item.orderId}&balance=${item.useBalance}`,
          });
        }
      }
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    wx.showLoading({
      title: '加载中...',
    })
    this.setData({
      'more': 1,
      'pagenum': this.data.pagenum + 1,
    });
    this.quorder();
  }
})